<?php
// +----------------------------------------------------------------------
// | 众联TK架构 [ ZoomTk ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2037 成都众联思索科技有限公司 [ http://www.zoomtk.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.sopvip.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace plugins\ImChat\controller;

use app\common\builder\TKuitpl;
use app\common\controller\Common;

/**
 * 插件后台管理控制器
 * @package plugins\HelloWorld\controller
 */
class ImChat extends Common
{
    /**
     * 插件首页
     * @author Sunday <153788231@qq.com>
     * @return mixed
     */
    public function index()
    {
        $config = plugin_config('ImChat');
        if($config['status']){
            $this->assign('imchat', $config);
            return $this->pluginView('imchat');
         //   $this->fetch('imchat');
         //   return $this->fetch();
        }
//        $mine = db('chatuser')->where('id', cookie('uid'))->find();
//        $this->assign([
//            'uinfo' => $mine
//        ]);
//        return $this->fetch();

    }

    /**
     * 保存聊天记录消息
     * @author Sunday <153788231@qq.com>
     * @return mixed
     */
    public function send_msg()
    {
        $member = array();
        $model_chat = Model('web_chat');
        if (empty($_POST)) $_POST = $_GET;
        $member_id = $_SESSION['member_id'];
        $member_name = $_SESSION['member_name'];
        $f_id = intval($_POST['f_id']);
        $t_id = intval($_POST['t_id']);
        $t_name = trim($_POST['t_name']);
        if (($member_id < 1) || ($member_id != $f_id)) $this->error(Language::get('nc_member_chat_login'));
        $member = $model_chat->getMember($t_id);
        if ($t_name != $member['member_name']) $this->error(Language::get('nc_member_chat_name_error'));

        $msg = array();
        $msg['f_id'] = $f_id;
        $msg['f_name'] = $member_name;
        $msg['t_id'] = $t_id;
        $msg['t_name'] = $t_name;
        $msg['t_msg'] = trim($_POST['t_msg']);
        if ($msg['t_msg'] != '') $chat_msg = $model_chat->addMsg($msg);
        if ($chat_msg['m_id']) {
            $this->json($chat_msg);
        } else {
            $this->error(Language::get('nc_member_chat_add_error'));
        }
    }

    //获取列表
    public function getList()
    {
        //查询自己的信息
        $mine = db('chatuser')->where('id', cookie('uid'))->find();
        $other = db('chatuser')->select();

        //查询当前用户的所处的群组
        $groupArr = [];
        $groups = db('groupdetail')->field('groupid')->where('userid', cookie('uid'))->group('groupid')->select();
        if( !empty( $groups ) ){
            foreach( $groups as $key=>$vo ){
                $ret = db('chatgroup')->where('id', $vo['groupid'])->find();
                if( !empty( $ret ) ){
                    $groupArr[] = $ret;
                }
            }
        }
        unset( $ret, $groups );

        $online = 0;
        $group = [];  //记录分组信息
        $userGroup = config('user_group');
        $list = [];  //群组成员信息
        $i = 0;
        $j = 0;

        foreach( $userGroup as $key=>$vo ){
            $group[$i] = [
                'groupname' => $vo,
                'id' => $key,
                'online' => 0,
                'list' => []
            ];
            $i++;
        }
        unset( $userGroup );

        foreach( $group as $key=>$vo ){

            foreach( $other as $k=>$v ) {

                if ($vo['id'] == $v['groupid']) {

                    $list[$j]['username'] = $v['username'];
                    $list[$j]['id'] = $v['id'];
                    $list[$j]['avatar'] = $v['avatar'];
                    $list[$j]['sign'] = $v['sign'];

                    if ('online' == $v['status']) {
                        $online++;
                    }

                    $group[$key]['online'] = $online;
                    $group[$key]['list'] = $list;

                    $j++;
                }
            }
            $j = 0;
            $online = 0;
            unset($list);
        }
        //print_r($group);die;
        unset( $other );

        $return = [
            'code' => 0,
            'msg'=> '',
            'data' => [
                'mine' => [
                    'username' => $mine['username'],
                    'id' => $mine['id'],
                    'status' => 'online',
                    'sign' => $mine['sign'],
                    'avatar' => $mine['avatar']
                ],
                'friend' => $group,
                'group' => $groupArr
            ],
        ];
        return json( $return );

    }
    //获取组员信息
    public function getMembers()
    {
        $id = input('param.id');

        //群主信息
        $owner = db('chatgroup')->field('owner_name,owner_id,owner_avatar,owner_sign')->where('id = ' . $id)->find();
        //群成员信息
        $list = db('groupdetail')->field('userid id,username,useravatar avatar,usersign sign')
            ->where('groupid = ' . $id)->select();
        $return = [
            'code' => 0,
            'msg' => '',
            'data' => [
                'owner' => [
                    'username' => $owner['owner_name'],
                    'id' => $owner['owner_id'],
                    'owner_id' => $owner['owner_avatar'],
                    'sign' => $owner['owner_sign']
                ],
                'list' => $list
            ]
        ];
        return json( $return );
    }
}
